package com.abysshal.icarte.datamodel.persistence;

import org.hibernate.Session;
import org.hibernate.Transaction;

public class GenericService {
	public static enum ReturnType{
		SUCC, FAIL, INTERNAL_ERROR
	}
	public ReturnType save(Object o){
		Session session = HibernateUtil.getSession();
		Transaction tr = session.beginTransaction();
		try {
			session.save(o);
			tr.commit();
		} catch (Exception ex){
			ex.printStackTrace();
			tr.rollback();
			return ReturnType.FAIL;
		}
		return ReturnType.SUCC;
	}

	public ReturnType update(Object o){
		Session session = HibernateUtil.getSession();
		Transaction tr = session.beginTransaction();
		try {
			session.saveOrUpdate(o);
			tr.commit();
		} catch (Exception ex){
			ex.printStackTrace();
			tr.rollback();
			return ReturnType.FAIL;
		}
		return ReturnType.SUCC;
	}
	
	public Object get(Class cls, Long id){
		Session session = HibernateUtil.getSession();
		Transaction tr = session.beginTransaction();
		Object o = null;
		try {
			o = session.get(cls, id);
			tr.commit();
		} catch (Exception ex){
			ex.printStackTrace();
			tr.rollback();
			return null;
		}
		return o;
	}
}
